import time
from datetime import datetime

import pandas
import pandas as pd
import geopandas as geo
import matplotlib.pyplot as plt
from math import cos, sin, atan2, sqrt, pi ,radians, degrees

# pandas show all rows
pd.set_option('display.max_rows', None)
# pandas show all col
pd.set_option('display.max_columns', 20)
# dataframe打印不换行
pd.set_option('display.width', 5000)

#####################################################
# clean data first step
# delete taxi orders with travel less than 5 minutes    删除订单时间小于5分钟的行程
# cleans orders where uplon or uplat or offlon or offlat == 0   删除上下车站的经纬度等于0的订单
#####################################################

start = time.time()

# read taxi order data
taxi_order = pd.read_csv('F:/ChromeDown/yellow_tripdata_2014-01.csv',usecols=[' pickup_datetime',' dropoff_datetime',' passenger_count',' pickup_longitude',' pickup_latitude',' dropoff_longitude',' dropoff_latitude'])
print(taxi_order.shape)
taxi_order.columns = ['up_time','off_time','user_count','up_lon','up_lat','off_lon','off_lat']
print(taxi_order.head())

# temp = taxi_order[taxi_order['up_lon']==0]
# print(temp.head())
# print(temp.shape)

taxi_order.drop(taxi_order[taxi_order['up_lon']==0].index,inplace=True)
taxi_order.drop(taxi_order[taxi_order['up_lat']==0].index,inplace=True)
taxi_order.drop(taxi_order[taxi_order['off_lon']==0].index,inplace=True)
taxi_order.drop(taxi_order[taxi_order['off_lat']==0].index,inplace=True)
print(taxi_order.shape)
print(taxi_order.head())


# delete orders with travel less than 5 minutes
taxi_order.up_time = pd.to_datetime(taxi_order['up_time'],format='%Y-%m-%d %H:%M:%S')
taxi_order.off_time = pd.to_datetime(taxi_order['off_time'],format='%Y-%m-%d %H:%M:%S')
taxi_order['diff_time'] = (taxi_order['off_time']-taxi_order['up_time']).dt.seconds

taxi_order.drop(taxi_order[taxi_order['diff_time']<300].index,inplace=True)
taxi_order.drop(['diff_time'],axis=1,inplace=True)
taxi_order.set_index(['up_time'],inplace=True)
taxi_order.sort_index(inplace=True)
print(taxi_order.shape)
print(taxi_order.head())

# temp = taxi_order[taxi_order['up_lon']==0]
# print(temp)

taxi_order.to_csv('E:/DataSet/taxi_data.csv')

print('run time:',time.time()-start,'s')